var login_user = null
var onechannelid = null;
$(function() { // 初始化内容
	mui.plusReady();
	mui.init();
	tap_init();
	mask = mui.createMask(); //遮罩层

	login_user = JSON.parse(localStorage.getItem('loginUser'));
	datetime_init();

	window.setTimeout(function() {
		initClient();

	}, 500);



})


function tap_init() {

	jQuery(document).ready(function() {

		jQuery("#onechannelid").change(function() {
			var onechannelid = jQuery("#onechannelid").val()

			gettowChannel(onechannelid, "")
		});

	});


}

function initClient() {
	var data = {};

	console.log("初始划客户上传=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/newkh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,

		success: function(res) {

			console.log("初始化信息=====" + JSON.stringify(res));
			var onechannelList = res.onechannelList
			jQuery("#id").val(res.newxlc_stu.id)

			jQuery("#onechannelid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < onechannelList.length; j++) {
				str += "<option value=\"" + onechannelList[j].id + "\" >" + onechannelList[j].channelname + "</option>";
			}

			jQuery("#onechannelid").append(str);
			
			jQuery("#yxschoolid").find("option").remove();
			var schoolList = res.fxlist;
			var schoolStr = "<option value=\"\">请选择</option>";
			for (var j = 0; j < schoolList.length; j++) {
				schoolStr += "<option value=\"" + schoolList[j].id + "\" >" + schoolList[j].schoolname + "</option>";
			}
			
			jQuery("#yxschoolid").append(schoolStr);
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});

}


function saveClient() {



	var data = jQuery("#new_kh_form").serializeObject();;
	data['kfyyperson'] = login_user.id
	data['kfyypersonname'] = login_user.name
	data['serviceid'] = login_user.id
	data['servicename'] = login_user.name
	console.log("新建客户上传信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/savekh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {


			mask.close(); //显示遮罩层 	  
			mui.alert("保存成功")
			setTimeout(function() {
				mui.back()
			}, 1000)
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}

function gettowChannel(oneChannelId, towChannelidVal) {
	var data = {};
	data["onechannelid"] = oneChannelId
	console.log("获取二级渠道下拉=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "newxlc_channel/getTwoChannel",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {

			mask.close(); //显示遮罩层 	  
			jQuery("#twochannelid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < res.length; j++) {
				str += "<option value=\"" + res[j].id + "\" >" + res[j].channelname + "</option>";
			}

			jQuery("#twochannelid").append(str);
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	}).done(function() {
		if (towChannelidVal != '') {
			jQuery("#twochannelid").val(towChannelidVal);
		}

	});
}


